provider "alicloud" {
  region                = local.region
}

terraform {
  required_version      = "~> 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "prod/services/dtu"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

module "random" {
  source        = "../../../modules/alicloud/random-vswitch"
  vswitch_id    = ["vsw-wz9w4iuvcnwa03axrw82h", "vsw-wz9fkew6pre4kz7lowp1g", "vsw-wz9wggzrf1yhagzzyn80b"]
}

variable "vpc" {
  default      = "vpc-wz9abg6b3gsprwwe2gh0i"
}

module "sg" {
  source                = "../../../modules/alicloud/sg"
  sg_name               = local.fullname
  vpc_id                = var.vpc
  inbound_rules         = {
    "0" = ["0.0.0.0/0", "22/22", "tcp"]
    # "1" = ["0.0.0.0/0", "80/80", "tcp"]
    # "2" = ["0.0.0.0/0", "443/443", "tcp"]
    # "3" = ["0.0.0.0/0", "2379/2379", "tcp"]
  }
}

module "ecs_dtu-gateway" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-gateway"
  ecs_hostname                 = "drrun-gateway"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz97jg8n9dsgrmzahbgt"  //自定义镜像dtu-gateway
  ecs_max_bandwidth_out        = "20"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-schedule" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-schedule"
  ecs_hostname                 = "drrun-schedule"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-vehicle" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-vehicle"
  ecs_hostname                 = "drrun-vehicle"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-map" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-map"
  ecs_hostname                 = "drrun-map"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.g6e.4xlarge"// "16h32g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}
module "ecs_dtu-notification" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-notification"
  ecs_hostname                 = "drrun-notification"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-base" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-base"
  ecs_hostname                 = "drrun-base"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.4xlarge"// "16h32g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-rocketmq" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-rocketmq"
  ecs_hostname                 = "drrun-caocao"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-dongfeng" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-dongfeng"
  ecs_hostname                 = "drrun-dongfeng"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

# module "ecs_dtu-coordination_map" {
#   source                       = "../../../modules/alicloud/ecs"
#   number_of_instances          = "1"
#   ecs_name                     = "prod-dtu-coordination-map"
#   ecs_hostname                 = "drrun-coordination-map"
#   ecs_vswitch_id               = module.random.vswitch_id[0]
#   ecs_sg_id                    = [module.sg.sg_id]
#   ecs_key_name                 = "devops-key"
#   ecs_instance_type            = "ecs.c6e.2xlarge"// "8h16g"   
#   ecs_disk_type                = "cloud_essd"         
#   ecs_disk_size                = "80"
#   ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
#   ecs_max_bandwidth_out        = "5"
#   ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
#   ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
#   prepaid_settings             = {
#     "period"             = "1"
#     "period_unit"        = "Month"
#     "renewal_status"     = "NotRenewal" //不续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
#     "auto_renew_period"  = "1"
#     "include_data_disks" = "true"
#   }
#   spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
#   role_name                    = "prod-infrastructure-role"
#   tags                         = local.tags
#   userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
#   userdata_VAR_PLAYBOOKS       = [
#     "playbooks/common/load-devops-keys.yml",
#   ]  
# }

module "ecs_dtu-coordination_map1" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-coordination-map1"
  ecs_hostname                 = "drrun-coordination-map"
  ecs_vswitch_id               = module.random.vswitch_id[1]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.hfg7.2xlarge"// "8h16g"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

module "ecs_dtu-account" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "1"
  ecs_name                     = "prod-dtu-account"
  ecs_hostname                 = "drrun-account"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6e.2xlarge"// "4h48"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "80"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}

# module "redis" {
#   source                  = "../../../modules/alicloud/redis"
#   zone_id                 = "cn-shenzhen-d"
#   instance_name           = local.fullname
#   instance_type           = "Redis"
#   engine_version          = "5.0"
#   instance_class          = "redis.amber.master.mid.multithread" #redis.amber.logic.sharding.1g.2db.0rodb.6proxy.multithread"  //ref: https://help.aliyun.com/document_detail/26350.html?spm=a2c4g.11186623.6.580.4d2d754eqDUHep
#   vswitch_id              = module.random.vswitch_id[0]
#   security_group_id       = module.sg.sg_id
#   security_ips            = []
#   kms_encrypted_password  = "NzFlYjA3OTMtYTk0Zi00YmZjLTgzOWQtNDdlZjE3MGJiMmY0367pSBrg3QWUk0hlARULWz3v0+0hBFGn+CJkP/ypnNoIpiWB4LhbvPKNZIsMo135ML05UJA1tTs="
#   password                = "NzFlYjA3OTMtYTk0Zi00YmZj"
#   tags                    = local.tags
# }

module "rds" {
  source                      = "../../../modules/alicloud/rds/rds-instance"
  rds_instance_name           = local.fullname
  rds_vswitch_id              = [module.random.vswitch_id[0]]
  rds_security_group_ids      = [module.sg.sg_id]
  tags                        = local.tags
  rds_security_ips            = ["127.0.0.1"]
  rds_engine                  = "MySQL"
  rds_engine_version          = "8.0"
  rds_instance_type           = "mysql.n1.micro.1"  //https://help.aliyun.com/document_detail/26312.html?spm=a2c4g.11186623.6.570.37b93fa0e1d4lf
  rds_instance_storage        = "50"
  rds_instance_charge_type    = "Postpaid"
  rds_username                = "superadmin"
  rds_kms_password            = "MDBkM2FjZTItNjExMC00ZDVhLWJiNjktMTZkNTBhYzkxNTlk/H2cTERJTdzZSiwqha4VXK7TdU326L5oeZd2mOViw5B7wvqTYLbaQFHYfdIgRekGgg8aS9AWRHs="
  enable_rds_backup           = true
  rds_parameters              = [
    {
      name                    = "max_connect_errors"
      value                   = "100000"
    },    
    {
    name                      = "slow_launch_time"
    value                     = "1"
    },
  ]    
}

module "rds_database" {
  source                      = "../../../modules/alicloud/rds/rds-databases"
  rds_instance_id             = module.rds.rds_id
  rds_database_name           = "map"
}

# resource "alicloud_ons_instance" "main" {
#   instance_name = local.fullname
#   remark        = "terrform manager rocketmq instance"
#   tags          = local.tags
# }

# resource "alicloud_ons_group" "main" {
#   group_name  = "GID-dtu"
#   instance_id = alicloud_ons_instance.main.id
#   remark      = "terrform manager rocketmq instance"
# }

# resource "alicloud_ons_topic" "main" {
#   topic_name   = "dtu"
#   instance_id  = alicloud_ons_instance.main.id
#   message_type = 0  #"0: normal messages 1: partitionally ordered messages 2: globally ordered messages 4: transactional messages 5: scheduled or delayed messages"
#   perm         = 6  #	设置该Topic的读写模式。取值说明如下：6：同时支持读写4：禁写2：禁读
#   remark       = "terraform manager rocketmq instance"
# }

module "ecs_dtu-etcd-cluster" {
  source                       = "../../../modules/alicloud/ecs"
  number_of_instances          = "3"
  ecs_name                     = "prod-dtu-etcd-cluster"
  ecs_hostname                 = "drrun-etcd-cluster"
  ecs_vswitch_id               = module.random.vswitch_id[0]
  ecs_sg_id                    = [module.sg.sg_id]
  ecs_key_name                 = "devops-key"
  ecs_instance_type            = "ecs.c6.xlarge"// "4h48"   
  ecs_disk_type                = "cloud_essd"         
  ecs_disk_size                = "200"
  ecs_image                    = "m-wz94ddsnh30cn2w54d3x"
  ecs_max_bandwidth_out        = "5"
  ecs_internet_billing_type    = "PayByTraffic"    #带宽按使用流量付费[PayByTraffic、PayByBandwidth]
  ecs_instance_billing_type    = "PrePaid"        #ecs按量后付费[PostPaid、PrePaid]
  prepaid_settings             = {
    "period"             = "1"
    "period_unit"        = "Month"
    "renewal_status"     = "AutoRenewal" //自动续订                                                                                                                                                                                                                                                                                                                                                                                                                                         
    "auto_renew_period"  = "1"
    "include_data_disks" = "true"
  }
  spot_strategy                = "NoSpot"  #竞价型实例的价格阈值[NoSpot、SpotWithPriceLimit、SpotAsPriceGo]
  role_name                    = "prod-infrastructure-role"
  tags                         = local.tags
  userdata_TERRAFORM_OSSBUCKET = "terraform-resources"
  userdata_VAR_PLAYBOOKS       = [
    "playbooks/common/load-devops-keys.yml",
  ]  
}